﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using Components;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Pot0a01Manager : Pot0a01Base
    {
        private static Pot0a01Manager _entity;

        public static Pot0a01Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Pot0a01Manager();
                return _entity;
            }
        }

        #region 个人代码

        public string SaveData(Hashtable tb, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Pot0a01 model = null;
            if ("A" == action)
            {
                model = new Pot0a01();
                Components.CommonUtil.GetInstance.CopyProperties(tb, model);
                //添加维护者信息
                model.Muser = Context.GetAppByUserCode(usercode);

                //生成主键标识
                model.Listid = Sys0a60Manager.Entity.CreateSn("pot0a01_listid", usercode);
                //执行新增
                lst.Add(this.Add(model));
            }
            else if ("M" == action)
            {
                model = this.GetModelById(Components.Validation.ToString(tb["listid"]));

                if (model == null) return string.Format("商品结构代码({0})可能已经被删除，请重新操作！",
                    tb["levercode"]);

                Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                lst.Add(this.Update(model));
            }
            else if ("D" == action)
            {
                string id = Components.Validation.ToString(tb["listid"]);

                List<SqlApp> tmp = GetLeverChilds(id);
                if (null != tmp)
                {
                    lst.AddRange(tmp.ToArray());
                }
            }

            return CommonManager.Entity.SaveData(lst);
        }

        private List<SqlApp> GetLeverChilds(string tid)
        {
            List<SqlApp> tmp = null;
            Hashtable hs = new Hashtable();
            hs["id"] = tid;
            DataTable areas = CommonManager.Entity.ExecDBFun("getleverclilds", hs);
            if (areas == null || 0 == areas.Rows.Count) return null;

            tmp = new List<SqlApp>();
            string id = string.Empty;
            foreach (DataRow dr in areas.Rows)
            {
                id = Validation.ToString(dr["alistid"]);

                tmp.AddRange(DeleteById(id).ToArray());
            }

            return tmp;
        }

        private List<SqlApp> DeleteById(string id)
        {
            List<SqlApp> lst = new List<SqlApp>();
            lst.Add(pot0a01Dal.DeleteAllByListId(id));
            lst.Add(pot0a01Dal.DeleteAllById(id));
            lst.Add((new Pot3a00DAL()).DeleteAllByLeverId(id));

            return lst;
        }

        #endregion
    }
}



